package com.baizhi.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;

@Controller
public class ValidateCodeController extends HttpServlet {
	@RequestMapping("/validatecode")
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(120, 40);
		String code = lineCaptcha.getCode();
		System.out.println(code);
		HttpSession session = req.getSession();
		session.setAttribute("code", code);
		OutputStream os = resp.getOutputStream();
		lineCaptcha.write(os);
	}
}
